package my.dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import my.utils.DBUtils;
import my.bean.Admin;
import my.dao.IAdminDao;

public class IAdminDaoImpl implements IAdminDao {

	private Connection conn;

	public IAdminDaoImpl() {

	}

	public IAdminDaoImpl(Connection conn) {
		this.conn = conn;
	}

	public void setConnection(Connection conn) {
		this.conn = conn;
	}

	@Override
	public int add(Admin admin) throws SQLException {
		PreparedStatement prestmt = null;
		int record = 0;

		try {
			String sql = "insert into Admin values(admin_id.nextval,?,?,?,?,?)";
			prestmt = conn.prepareStatement(sql);
			prestmt.setString(1, admin.getAccount());
			prestmt.setString(2, admin.getPassword());
			prestmt.setString(3, admin.getEmail());
			prestmt.setString(4, admin.getPhone());
			prestmt.setTimestamp(5, new Timestamp(admin.getRegDate().getTime()));

			record = prestmt.executeUpdate();

			if (record > 0) {
				System.out.println("注册成功,请登录!");
			} else {
				System.out.println("注册失败,请重新注册");
			}
			return record;
		} catch (SQLException e) {
			System.out.println("插入失败,请重新操作,谢谢.");
			throw e;
		} finally {
			DBUtils.close(null, prestmt, null);
		}
	}

	@Override
	public int deleteAdminById(int adminId) throws SQLException {
		PreparedStatement prestmt = null;
		int record = 0;
		try {
			String sql = "delete from Admin where admin_id=?";
			prestmt = conn.prepareStatement(sql);
			prestmt.setInt(1, adminId);

			record = prestmt.executeUpdate();

			if (record > 0) {
				System.out.println("删除成功!");
			} else {
				System.out.println("删除失败!请重新操作");
			}

			return record;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally {
			DBUtils.close(null, prestmt, null);
		}

	}

	@Override
	public int modify(Admin admin) throws SQLException {
		PreparedStatement prestmt = null;
		int record = 0;
		try {
			String sql = "update Admin set admin_account=?,admin_email=?,admin_phone=? where admin_id=?";
			prestmt = conn.prepareStatement(sql);
			prestmt.setString(1, admin.getAccount());
			prestmt.setString(2, admin.getEmail());
			prestmt.setString(3, admin.getPhone());
			prestmt.setInt(4, admin.getAdminId());

			record = prestmt.executeUpdate();

			if (record > 0) {
				System.out.println("修改成功!");
			} else {
				System.out.println("修改失败!请重新操作!");
			}

			return record;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally {
			DBUtils.close(null, prestmt, null);
		}

	}

	@Override
	public List<Admin> findAll() {
		PreparedStatement prestmt = null;
		ResultSet rs = null;
		List<Admin> list = new ArrayList<>();

		try {
			String sql = "select * from Admin";
			prestmt = conn.prepareStatement(sql);

			rs = prestmt.executeQuery();

			while (rs.next()) {
				int cid = rs.getInt(1);
				String account = rs.getString(2);
				String password = rs.getString(3);
				String email = rs.getString(4);
				String phone = rs.getString(5);
				Timestamp regDate = rs.getTimestamp(6);

				Admin admin = new Admin(cid, account, password, email, phone, regDate);
				list.add(admin);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.close(null, prestmt, rs);
		}
		return list;
	}

	@Override
	public Admin findByAdminId(int adminId) {
		PreparedStatement prestmt = null;
		Admin admin = null;
		ResultSet rs = null;
		try {
			String sql = "select * from Admin where admin_id=?";
			prestmt = conn.prepareStatement(sql);
			prestmt.setInt(1, adminId);

			rs = prestmt.executeQuery();

			if (rs.next()) {
				int cid = rs.getInt(1);
				String account = rs.getString(2);
				String password = rs.getString(3);
				String email = rs.getString(4);
				String phone = rs.getString(5);
				Timestamp regDate = rs.getTimestamp(6);

				admin = new Admin(cid, account, password, email, phone, regDate);
			}

			return admin;

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.close(null, prestmt, rs);
		}
		return admin;
	}

	@Override
	public int modifyPassword(Admin admin) throws SQLException {
		PreparedStatement prestmt = null;
		int record = 0;

		try {
			String sql = "update Admin set password=? where admin_id=?";
			prestmt = conn.prepareStatement(sql);
			prestmt.setString(1, admin.getPassword());
			prestmt.setInt(2, admin.getAdminId());

			record = prestmt.executeUpdate();

			if (record > 0) {
				System.out.println("密码修改成功!");
			} else {
				System.out.println("密码修改失败!请重新操作!");
			}

			return record;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally {
			DBUtils.close(null, prestmt, null);
		}
	}

	@Override
	public Admin findByAdminAccount(String accout) {
		PreparedStatement prestmt = null;
		ResultSet rs = null;
		Admin admin = null;
		try {
			String sql = "select * from admin where admin_account=?";
			prestmt = conn.prepareStatement(sql);
			prestmt.setString(1, accout);

			rs = prestmt.executeQuery();

			while (rs.next()) {
				int cid = rs.getInt(1);
				String account = rs.getString(2);
				String password = rs.getString(3);
				String email = rs.getString(4);
				String phone = rs.getString(5);
				Timestamp regDate = rs.getTimestamp(6);

				admin = new Admin(cid, account, password, email, phone, regDate);
			}
			return admin;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.close(null, prestmt, rs);
		}

		return admin;
	}

	@Override
	public Admin findByAccountAndPassword(String account, String password) {
		PreparedStatement prestmt = null;
		ResultSet rs = null;
		Admin admin = null;

		try {
			String sql = "select * from admin where admin_account=? and trim(admin_password)=?";
			prestmt = conn.prepareStatement(sql);
			prestmt.setString(1, account);
			prestmt.setString(2, password);

			rs = prestmt.executeQuery();
			if (rs.next()) {
				int cid = rs.getInt(1);
				account = rs.getString(2);
				password = rs.getString(3);
				String email = rs.getString(4);
				String phone = rs.getString(5);
				Timestamp regDate = rs.getTimestamp(6);

				admin = new Admin(cid, account, password, email, phone, regDate);
			}
			return admin;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.close(null, prestmt, null);
		}
		return admin;
	}

}
